package com.ruoyi.web.controller.market.member;

import cn.hutool.core.util.StrUtil;
import com.alibaba.fastjson.JSONObject;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.db.domain.MemberExtInfo;
import com.ruoyi.db.model.param.ChannelInfoParam;
import com.ruoyi.db.model.param.MemberExtInfoParam;
import com.ruoyi.extend.service.IChannelInfoService;
import com.ruoyi.market.enums.SpecialEnum;
import com.ruoyi.member.service.IMemberExtInfoService;
import com.ruoyi.web.service.IUserPerimService;
import com.ruoyi.web.wapper.MemberExtInfoWapper;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDate;
import java.util.List;
import java.util.Map;

/**
 * 用户扩展信息Controller
 * 
 * @author ruoyi
 * @date 2022-08-02
 */
@Controller
@RequestMapping("/member/extInfo")
public class MemberExtInfoController extends BaseController
{
    private String prefix = "member/extInfo";

    @Autowired
    private IMemberExtInfoService memberExtInfoService;
    @Autowired
    private IChannelInfoService channelInfoService;
    @Autowired
    private IUserPerimService userPerimService;

    @RequiresPermissions("member:extInfo:view")
    @GetMapping()
    public String extInfo(ModelMap mmap)
    {

        mmap.put("loanAmount", SpecialEnum.BaseTypeInfo.loanAmount);

        mmap.put("zhimaScore", SpecialEnum.BaseTypeInfo.zhimaScore);
        mmap.put("huaBei", SpecialEnum.BaseTypeInfo.huaBei);
        mmap.put("baiTiao", SpecialEnum.BaseTypeInfo.baiTiao);

        mmap.put("gjj", SpecialEnum.BaseTypeInfo.gjj);
        mmap.put("house", SpecialEnum.BaseTypeInfo.house);
        mmap.put("car", SpecialEnum.BaseTypeInfo.car);
        mmap.put("insurance", SpecialEnum.BaseTypeInfo.insurance);
        mmap.put("socialInsurance", SpecialEnum.BaseTypeInfo.socialInsurance);

        // 查询所有渠道的渠道信息
        ChannelInfoParam channelInfoParam = new ChannelInfoParam();
        // 查询渠道号信息
        List<Map<String, String>> channelCodeList = channelInfoService.selectSimpleMapInfo(channelInfoParam);
        mmap.put("channelCodeList", channelCodeList);

        return prefix + "/extInfo";
    }

    /**
     * 查询用户扩展信息列表
     */
    @RequiresPermissions("member:extInfo:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(MemberExtInfoParam memberExtInfo)
    {
        startPage();

        List<Map<String, Object>> mapList = memberExtInfoService.selectMemberExtInfoListMap(memberExtInfo);
        mapList = new MemberExtInfoWapper(mapList).wrap();

        // 敏感数据不展示
        List<String> userInfoSUnVisibleColumnsList = userPerimService.getUserInfoSUnVisibleColumnsList();
        JSONObject extData = new JSONObject();
        extData.put("unVisibleColumns", userInfoSUnVisibleColumnsList);

        TableDataInfo dataTable = getDataTable(mapList);
        dataTable.setExtData(extData);

        return dataTable;
    }

    /**
     * 导出用户扩展信息列表
     */
    @RequiresPermissions("member:extInfo:export")
    @Log(title = "用户扩展信息", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(MemberExtInfo memberExtInfo)
    {
        List<MemberExtInfo> list = memberExtInfoService.selectMemberExtInfoList(memberExtInfo);
        ExcelUtil<MemberExtInfo> util = new ExcelUtil<MemberExtInfo>(MemberExtInfo.class);
        return util.exportExcel(list, "用户扩展信息数据");
    }

    /**
     * 新增用户扩展信息
     */
    @GetMapping("/add")
    public String add()
    {
        return prefix + "/add";
    }

    /**
     * 新增保存用户扩展信息
     */
    @RequiresPermissions("member:extInfo:add")
    @Log(title = "用户扩展信息", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(MemberExtInfo memberExtInfo)
    {
        return toAjax(memberExtInfoService.insertMemberExtInfo(memberExtInfo));
    }

    /**
     * 修改用户扩展信息
     */
    @RequiresPermissions("member:extInfo:edit")
    @GetMapping("/edit/{uid}")
    public String edit(@PathVariable("uid") Long uid, ModelMap mmap)
    {
        MemberExtInfo memberExtInfo = memberExtInfoService.selectMemberExtInfoByUid(uid);
        mmap.put("memberExtInfo", memberExtInfo);
        return prefix + "/edit";
    }

    /**
     * 修改保存用户扩展信息
     */
    @RequiresPermissions("member:extInfo:edit")
    @Log(title = "用户扩展信息", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(MemberExtInfo memberExtInfo)
    {
        return toAjax(memberExtInfoService.updateMemberExtInfo(memberExtInfo));
    }

    /**
     * 删除用户扩展信息
     */
    @RequiresPermissions("member:extInfo:remove")
    @Log(title = "用户扩展信息", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(memberExtInfoService.deleteMemberExtInfoByUids(ids));
    }


    @PostMapping( "/count")
    @ResponseBody
    public AjaxResult count(@RequestParam("starttime") String starttime, @RequestParam("endtime") String endtime)
    {
        return AjaxResult.success(memberExtInfoService.selectCountByDatetime(starttime, endtime));
    }

    /**
     * 图表数据
     */
    @GetMapping("/chartMapProvince")
    public String chartMapProvince()
    {
        return prefix + "/chartMapProvince";
    }

    @PostMapping( "/chartMapProvinceData")
    @ResponseBody
    public AjaxResult chartMapProvinceData(String dateInfo)
    {
        String startDate = LocalDate.now().toString();
        String endDate = LocalDate.now().toString();
        if (StrUtil.isNotEmpty(dateInfo)){
            String[] split = dateInfo.split("~");
            startDate = StrUtil.trim(split[0]);
            endDate = StrUtil.trim(split[1]);
        }
        JSONObject resultData = memberExtInfoService.getMapProvinceDataList(startDate, endDate);
        return AjaxResult.success(resultData);
    }

    /**
     * 图表数据
     */
    @GetMapping("/chartMapCity")
    public String chartMapCity(ModelMap modelMap,
                               @RequestParam("startDate") String startDate,
                               @RequestParam("endDate") String endDate,
                               @RequestParam("provinceCode") String provinceCode)
    {
        modelMap.put("provinceCode", provinceCode);
        modelMap.put("startDate", startDate);
        modelMap.put("endDate", endDate);

        return prefix + "/chartMapCity";
    }

    @PostMapping( "/chartMapCityData")
    @ResponseBody
    public AjaxResult chartMapCityData(
            @RequestParam("startDate") String startDate,
            @RequestParam("endDate") String endDate,
            @RequestParam("provinceCode") String provinceCode
    )
    {
        JSONObject resultData = memberExtInfoService.getMapCityDataList(startDate, endDate, provinceCode);
        return AjaxResult.success(resultData);
    }


    /**
     * 城市前100 分布
     */
    @GetMapping("/chartMap100City")
    public String chartMap100City()
    {
        return prefix + "/chartMap100City";
    }

    @PostMapping( "/chartMap100CityData")
    @ResponseBody
    public AjaxResult chartMap100CityData(String dateInfo)
    {
        String startDate = LocalDate.now().toString();
        String endDate = LocalDate.now().toString();
        if (StrUtil.isNotEmpty(dateInfo)){
            String[] split = dateInfo.split("~");
            startDate = StrUtil.trim(split[0]);
            endDate = StrUtil.trim(split[1]);
        }
        JSONObject resultData = memberExtInfoService.chartMap100CityData(startDate, endDate);
        return AjaxResult.success(resultData);
    }

}
